<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\school\controller;

use cmf\controller\AdminBaseController;
use app\school\model\SchoolCourseModel;
use think\Db;
use app\school\model\SchoolSubjectModel;
use app\school\model\SchoolLevelModel;
use app\school\model\SchoolClassModel;
use app\school\model\SchoolTeacherModel;

class AdminCourseController extends AdminBaseController
{    
    public function index()
    {
        $param = $this->request->param();

        $model = new SchoolCourseModel();
        $data = $model->lists($param);             
        $data->appends($param);
        $subjectModel = new SchoolSubjectModel();
        $subjectList = $subjectModel-> select();       
        $classModel = new SchoolClassModel();
        $classList = $classModel->  select();
        $teacherModel = new SchoolTeacherModel();
        $teacherList = $teacherModel-> select();
        
        $this->assign('subject_id', isset($param['subject_id']) ? $param['subject_id'] : '');
        $this->assign('class_id', isset($param['class_id']) ? $param['class_id'] : '');
        $this->assign('teacher_id', isset($param['teacher_id']) ? $param['teacher_id'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');

        $this->assign('subjectList',$subjectList);       
        $this->assign('classList',$classList);
        $this->assign('teacherList',$teacherList);  
        $this->assign('list',$data);
        $this->assign('page', $data->render());
        return $this->fetch();
    }

    public function add()
    {
        $subjectModel = new SchoolSubjectModel();
        $subjectList = $subjectModel->where('delete_time',0)->select();       
        $classModel = new SchoolClassModel();
        $classList = $classModel->where('delete_time',0)->select();
        $teacherModel = new SchoolTeacherModel();
        $teacherList = $teacherModel->where('delete_time',0)->select();
        
        $this->assign('subjectList',$subjectList);       
        $this->assign('classList',$classList);
        $this->assign('teacherList',$teacherList);
        return $this->fetch();
    }

    public function addPost()
    {
        $schoolCourseModel = new SchoolCourseModel();

        $data = $this->request->param();

        $result = $this->validate($data, 'SchoolCourse');

        if ($result !== true) {
            $this->error($result);
        }
        
        $result = $schoolCourseModel->allowField(true)->save($data);

        if ($result === false) {
            $this->error('添加失败!');
        }

        $this->success('添加成功!', url('AdminCourse/index'));

    }

    public function edit()
    {
        $subjectModel = new SchoolSubjectModel();
        $subjectList = $subjectModel->where('delete_time',0)->select();
        $classModel = new SchoolClassModel();
        $classList = $classModel->where('delete_time',0)->select();
        $teacherModel = new SchoolTeacherModel();
        $teacherList = $teacherModel->where('delete_time',0)->select();
        
        $this->assign('subjectList',$subjectList);
        $this->assign('classList',$classList);
        $this->assign('teacherList',$teacherList);

        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $course = SchoolCourseModel::get($id)->toArray();            
            $this->assign($course);            
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }
    }

    public function editPost()
    {      
        $data = $this->request->param();

        //需要抹除发布、置顶、推荐的修改。
        unset($data['status']);        
        unset($data['recommended']);

        $id          = intval($data['id']);
        $result = $this->validate($data, 'SchoolCourse');

        if ($result !== true) {
            $this->error($result);
        }

        $schoolCourseModel = new SchoolCourseModel();

        $result = $schoolCourseModel->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

        if ($result === false) {
            $this->error('保存失败!');
        }

        $this->success('保存成功!');
    }

    public function delete()
    {
        $SchoolCourseModel = new SchoolCourseModel();
        $id                  = $this->request->param('id');
        //获取删除的内容
        $findCourse = $SchoolCourseModel->where('id', $id)->find();

        if (empty($findCourse)) {
            $this->error('课程不存在!');
        }
//判断此分类有无子分类（不算被删除的子分类）
        
        $data   = [
            'object_id'   => $findCourse['id'],
            'create_time' => time(),
            'table_name'  => 'school_Course',
            'name'        => $findCourse['title']
        ];
        $result = $SchoolCourseModel
            ->where('id', $id)
            ->update(['delete_time' => time()]);
        if ($result) {
            Db::name('recycleBin')->insert($data);
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

    public function deleteList()
    {
        $SchoolCourseModel = new SchoolCourseModel();
        $ids = $this->request->param('ids/a');

        $result = $SchoolCourseModel
            ->where(['id' => ['in', $ids]])
            ->update(['delete_time' => time()]);
        if ($result) {           
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

    public function listOrder() 
    {        
        parent::listOrders(Db::name('school_course'));
        $this->success("排序更新成功！", '');
    }

    /**
     * 文章发布
     * @adminMenu(
     *     'name'   => '文章发布',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章发布',
     *     'param'  => ''
     * )
     */
    public function publish()
    {
        $param           = $this->request->param();
        $SchoolCourseModel = new SchoolCourseModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $SchoolCourseModel->where(['id' => ['in', $ids]])->update(['status' => 1, 'published_time' => time()]);

            $this->success("发布成功！", '');
        }

        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $SchoolCourseModel->where(['id' => ['in', $ids]])->update(['status' => 0]);

            $this->success("取消发布成功！", '');
        }

    }

    /**
     * 文章置顶
     * @adminMenu(
     *     'name'   => '文章置顶',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章置顶',
     *     'param'  => ''
     * )
     */
    public function isnew()
    {
        $param           = $this->request->param();
        $SchoolCourseModel = new SchoolCourseModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $SchoolCourseModel->where(['id' => ['in', $ids]])->update(['is_new' => 1]);

            $this->success("设置最新成功！", '');

        }

        if (isset($_POST['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $SchoolCourseModel->where(['id' => ['in', $ids]])->update(['is_new' => 0]);

            $this->success("取消最新成功！", '');
        }
    }

    /**
     * 文章推荐
     * @adminMenu(
     *     'name'   => '文章推荐',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章推荐',
     *     'param'  => ''
     * )
     */
    public function recommend()
    {
        $param           = $this->request->param();
        $SchoolCourseModel = new SchoolCourseModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $SchoolCourseModel->where(['id' => ['in', $ids]])->update(['recommended' => 1]);

            $this->success("推荐成功！", '');

        }
        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $SchoolCourseModel->where(['id' => ['in', $ids]])->update(['recommended' => 0]);

            $this->success("取消推荐成功！", '');

        }
    }   


}
